var md5 = require('md5');
var loadNativeImg = function(url, callback, index,error) { //这里还是要给参数
    //要进行一下分类
    let suffix = /\.[^\.]+$/.exec(url);
    // if(suffix=='png' || suffix=='jpg')

    var dirpath = jsb.fileUtils.getWritablePath() + 'img/';

    var filepath = dirpath + md5(url) + suffix; //'.jpg'; 

    function loadEnd() {
        cc.loader.load(filepath, function(err, tex) {
            if (err) {
                cc.error(err);
            } else {
                cc.log('index' + index);
                // if(index){
                callback(tex, index); //这样更灵活一点吧 
                // }else{
                //      callback(tex);
                // }

                // var spriteFrame = new cc.SpriteFrame(tex);
                // if( spriteFrame ){
                //     spriteFrame.retain();
                //     callback(spriteFrame);
                // }
            }
        });

    }

    if (jsb.fileUtils.isFileExist(filepath)) {
        cc.log('Remote is find' + filepath);
        loadEnd();
        return;
    }

    var saveFile = function(data) {
        if (typeof data !== 'undefined') {
            if (!jsb.fileUtils.isDirectoryExist(dirpath)) {
                jsb.fileUtils.createDirectory(dirpath);
            }

            if (jsb.fileUtils.writeDataToFile(new Uint8Array(data), filepath)) {
                cc.log('Remote write file succeed.');
                loadEnd();
            } else {
                cc.log('Remote write file failed.');
            }
        } else {
            cc.log('Remote download file failed.');
        }
    };

    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        cc.log("xhr.readyState  " + xhr.readyState);
        cc.log("xhr.status  " + xhr.status);
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {

                xhr.responseType = 'arraybuffer';
                saveFile(xhr.response);
            } else {
                saveFile(null);
            }
        }
    }.bind(this);
    xhr.ontimeout =function(){
        cc.log(index+' is timeout');
        error(index);
    }.bind(this);
    xhr.onerror =function(){
       // cc.log(index+' is error'+error);
        error(index);
    }.bind(this);
    xhr.open("GET", url, true);
    xhr.send();
};
//加载多个
var loadAll = function(urls, callback, progress,error) {

    if (toString.apply(urls) === '[object Array]') {
        var n = 0;
        var rs = []; //new Array(urls.length);
        let totalCount=urls.length;
        for (var i = urls.length - 1; i >= 0; i--) {

            loadNativeImg(urls[i], function(rc, index) {
                //完成了一个之个
                n = n + 1;
                rs[index] = rc;
                cc.log('index=' + index + '  rs[index]=' + rs[index]);
                progress(n/totalCount);
                if (n === urls.length) {
                    callback(rs);
                }
                
            }, i,error); //.then(cc.log('then!'));
            //urls[i]
        }
    }
};

//下载文件到本地
var downloadFile = function(url, dest, callback) {

};
//获取JSON
var requestJSON = function(url, callback) {};
//是不是要设置全部头信息，用于token
var setHead = function(key, value) {

};

var delFile = function(file, path, callback) {

};

var delPath = function(path, callback) {

};
//只取得头部信息
var loadHead = function(url, callback) {

};


var noop = function() {};

module.exports = {
    loadNativeImg: loadNativeImg,
    loadAll: loadAll,
    noop: noop
}